package com.kgc.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.server.dao.EasybuyProductCategoryDao;
import com.kgc.server.pojo.Easybuy_product_category;
import com.kgc.server.service.EasybuyProductCategoryService;
import com.kgc.server.util.Page;
import com.kgc.server.util.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EasybuyProductCategoryServiceImpl implements EasybuyProductCategoryService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private EasybuyProductCategoryDao easybuyProductCategoryDao;
    @Override
    public Easybuy_product_category getLeverByProId(String productId) {
        logger.info("EasybuyProductCategoryServiceImpl getLeverByProId params:"+productId);
        ResponseMessage responseMessage = new ResponseMessage();
        Easybuy_product_category leverByProId = easybuyProductCategoryDao.getLeverByProId(productId);
        responseMessage.setData(leverByProId);
        return leverByProId;
    }

    @Override
    public ResponseMessage getProductType() {
        logger.info("EasybuyProductCategoryServiceImpl getProductType start..");
        ResponseMessage msg = new ResponseMessage();
        List<Easybuy_product_category> p1 = new ArrayList<Easybuy_product_category>();
        List<Easybuy_product_category> p2 = new ArrayList<Easybuy_product_category>();
        List<Easybuy_product_category> p3 = new ArrayList<Easybuy_product_category>();
        p1 = easybuyProductCategoryDao.getProductType(0, 1);
            for(int i=0;i<p1.size();i++){
                Easybuy_product_category e = p1.get(i);
                p2=easybuyProductCategoryDao.getProductType(e.getId(),2);

                for(int y=0;y<p2.size();y++){
                    Easybuy_product_category e1 = p2.get(y);
                    System.out.println(p2.get(y));
                    p3=easybuyProductCategoryDao.getProductType(e1.getId(),3);
                    System.out.println(""+p3);
                    e1.setList(p3);
                }

                e.setList(p2);
            }
            msg.setCode("200");
            msg.setData(p1);

        return msg;
    }

    @Override
    public ResponseMessage getProductTypes(Integer parentId, Integer typeId) {
        logger.info("EasybuyProductCategoryServiceImpl getProductTypes params parentId:"+parentId+";typeId:"+typeId);
        ResponseMessage responseMessage = new ResponseMessage();
        List<Easybuy_product_category> productType = easybuyProductCategoryDao.getProductType(parentId, typeId);
        logger.info("EasybuyProductCategoryServiceImpl getProductTypes params parentId:"+parentId+";typeId:"+typeId+";result:"+productType);

        responseMessage.setData(productType);
        responseMessage.setMsg("获取type成功！");
        responseMessage.setCode("200");

        return responseMessage;
    }

    @Override
    public ResponseMessage showProductType3() {
        logger.info("EasybuyProductCategoryServiceImpl showProductType3 start...");
        ResponseMessage msg = new ResponseMessage();
        List<Easybuy_product_category> easybuy_product_categories = easybuyProductCategoryDao.showProductType3();
        logger.info("EasybuyProductCategoryServiceImpl showProductType3 start... result:"+easybuy_product_categories);
        msg.setMsg("获取成功！");
        msg.setData(easybuy_product_categories);
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMessage addProductType(Easybuy_product_category easybuy_product_category) {
        logger.info("EasybuyProductCategoryServiceImpl addProductType params:"+easybuy_product_category);
        ResponseMessage msg = new ResponseMessage();
        int i = easybuyProductCategoryDao.addProductType(easybuy_product_category);
        msg.setData(i);
        msg.setMsg("添加成功！");
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMessage showProductTypeByPage(String name, Integer type, Integer pageNo) {
        logger.info("EasybuyProductCategoryServiceImpl showProductTypeByPage params: name:"+name+";type:"+type+";pageNo"+pageNo);
        ResponseMessage msg = new ResponseMessage();
        Page page = new Page();
        PageHelper.startPage(pageNo,10);
        List<Easybuy_product_category> easybuy_product_categories = easybuyProductCategoryDao.showProductTypeByPage(name, type);
        logger.info("EasybuyProductCategoryServiceImpl showProductTypeByPage params: name:"+name+";type:"+type+";pageNo"+pageNo+";result:"+easybuy_product_categories);
        PageInfo<Easybuy_product_category> pageInfo = new PageInfo<>(easybuy_product_categories);
        page.setPageNo(pageNo);
        page.setPageCount(pageInfo.getNavigateLastPage());
        page.setPageSize(pageInfo.getPageSize());
        page.setList(pageInfo.getList());
        page.setCount(easybuyProductCategoryDao.showProductTypeByPage(name, type).size());
        msg.setData(page);
        msg.setMsg("查询成功！");
        msg.setCode("200");

        return msg;
    }

    @Override
    public ResponseMessage modifyType(Easybuy_product_category easybuy_product_category) {
        logger.info("EasybuyProductCategoryServiceImpl modifyType params :"+easybuy_product_category);
        ResponseMessage msg = new ResponseMessage();
        int i = easybuyProductCategoryDao.modifyType(easybuy_product_category);
        logger.info("EasybuyProductCategoryServiceImpl modifyType params :"+easybuy_product_category+";result:"+i);
        msg.setData(i);
        msg.setMsg("修改成功！");
        msg.setCode("200");
        return msg;
    }

    @Override
    public ResponseMessage delProductType(Integer id) {
        logger.info("EasybuyProductCategoryServiceImpl delProductType params:id"+id);
        ResponseMessage msg = new ResponseMessage();
        int productByIdNum = easybuyProductCategoryDao.getProductById(id);
        logger.info("EasybuyProductCategoryServiceImpl delProductType params:id"+id+" ; productByIdNum:"+productByIdNum);

        int sonTypeByIdNum = easybuyProductCategoryDao.getSonTypeById(id);
        logger.info("EasybuyProductCategoryServiceImpl delProductType params:id"+id+" ; sonTypeByIdNum:"+sonTypeByIdNum);
        if(sonTypeByIdNum+productByIdNum>0){
            msg.setMsg("还有数据删除不了");
            msg.setData(0);
            return msg;
        }
        int i = easybuyProductCategoryDao.delProductType(id);
        logger.info("EasybuyProductCategoryServiceImpl delProductType params:id"+id+";result:"+i);
        msg.setData(i);
        msg.setCode("200");
        msg.setMsg("删除成功！");
        return msg;
    }

    @Override
    public ResponseMessage getTypeById(Integer id) {
        logger.info("EasybuyProductCategoryServiceImpl getTypeById params: id: "+id);
        ResponseMessage msg = new ResponseMessage();
        Easybuy_product_category typeById = easybuyProductCategoryDao.getTypeById(id);
        logger.info("EasybuyProductCategoryServiceImpl getTypeById params: id: "+id+";result:"+typeById);
        msg.setMsg("查询成功！");
        msg.setData(typeById);
        msg.setCode("200");

        return msg;
    }


}
